﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Slja.Utility;
using Slja.Model;

namespace FrameWork.web.Web.UC
{
    public partial class ButtonTool : System.Web.UI.UserControl
    {
        OrderExtModel _Order;
        /// <summary>
        /// 订单实体类
        /// </summary>
        public OrderExtModel Order
        {
            get { return _Order; }
            set { _Order = value; }
        }
        public Btn BtnListState
        {
            get {
                if (ViewState["_BtnListState"] == null)
                {
                    ViewState["_BtnListState"] = Btn.Print;
                }
                return (Btn)ViewState["_BtnListState"];
            }
            set { ViewState["_BtnListState"] = value; }
        }
        public string ViewType
        {
            get
            {
                if (ViewState["_ViewType"] == null)
                {
                    ViewState["_ViewType"] = "";
                }
                return ViewState["_ViewType"].ToString().ToUpper();
            }
            set { ViewState["_ViewType"] = value; }
        }
        /// <summary>
        /// 申明按钮事件
        /// </summary>
        public event CommandEventHandler CommandHandle;
        protected void Page_Load(object sender, EventArgs e)
        { 	         
             if (Order.CurOpUserID == Common.Get_UserID)
             {
                 //BtnListState = Btn.Submit | Btn.Save | Btn.Print;


                 if (ViewType == "PUBLIC")
                 {
                     BtnListState = Btn.Print;
                 }
                 else
                 {
                     if (Order.Enum_SaleState == SaleState.DesignUp
                         || Order.Enum_SaleState == SaleState.PriceUp)
                     {
                         BtnListState = Btn.SendMail | Btn.Print;
                     }
                     else
                     {
                         BtnListState = Btn.Submit | Btn.Print;
                     }
                 }
             }
             ShowBotton(Panel1, BtnListState);
             if (BtnListState == Btn.None)
             {
                 this.Visible = false;
             }
             //Button btn = new Button();
             //btn.Command += new CommandEventHandler(btn_Command);
        }
        private void ShowBotton(Control ctr,Btn btnlist)
        {
            List<EnumUtility<Btn>.EnumInfo> li = EnumUtility<Btn>.GetEnumList();
            EnumUtility<Btn>.EnumInfo en;
            for (int i = 0; i < li.Count; i++)
            {
                en = li[i];
                if (en.Item != Btn.None && (btnlist & en.Item) == en.Item)
                {
                    Button b = new Button();
                    string name = en.Item.ToString();
                    b.ID = "btn_" + name;
                    b.Text = en.Descr;
                    //b.Click 
                    b.CssClass = "btnblue1";
                    b.CommandName = name;
                    b.OnClientClick = "return btnTool_" + name + "(this,'" + Order.ID+ "');";
                    b.Command += new CommandEventHandler(b_Command);
                    b.Height = new Unit(22,UnitType.Pixel);
                    ctr.Controls.Add(b);
                }
            }
        }

        void b_Command(object sender, CommandEventArgs e)
        {
            if (CommandHandle != null)
            {
                CommandHandle(sender, e);                
            }
        }
    }
}